import { EventEmitter } from 'eventemitter3';
import type { ItemId } from '../data/item';
import type { SpiritualRootKey } from '../model/spiritual-root';
import type { CultivationSkillItemId } from '../data/item/cultivation-skill';
import type { StoryId } from '../data/story';
import type { FeatureId } from '../data/feature-unlock/type';
import type { LevelUpEvent } from '../model/battle-level';
import type { Reward, RewardSourceType } from '../model/reward/type';
import type { SectScriptureListItem } from '../model/sect/type';
import type { InventorySlotItem, TempSlot } from '../model/inventory/type';
import type { ItemRarity } from '../data/item/type';

export const EVENTS = {
  /** 游戏开始 */
  GAME_START: 'GAME_START',
  /** 游戏停止 */
  GAME_STOP: 'GAME_STOP',
  /** 游戏销毁 */
  GAME_DESTROYED: 'GAME_DESTROYED',
  /** 游戏提示 */
  GAME_MESSAGE: 'GAME_MESSAGE',
  /** 自动保存 */
  GAME_AUTO_SAVE: 'GAME_AUTO_SAVE',
  /** 手动保存 */
  GAME_MANUAL_SAVE: 'GAME_MANUAL_SAVE',
  /** 保存错误 */
  GAME_SAVE_ERROR: 'GAME_SAVE_ERROR',
  /** 读档完成 */
  GAME_LOADED: 'GAME_LOADED',
  /** 加载错误 */
  GAME_LOAD_ERROR: 'GAME_LOAD_ERROR',
  /** 突破境界 */
  REALM_BREAKTHROUGH: 'REALM_BREAKTHROUGH',
  /** 修为状态更新 */
  REALM_STATE_UPDATE: 'REALM_STATE_UPDATE',
  /** 开始修炼 */
  CULTIVATION_START: 'CULTIVATION_START',
  /** 修炼状态更新 */
  CULTIVATION_STATE_UPDATE: 'CULTIVATION_STATE_UPDATE',
  /** 修炼完成 */
  CULTIVATION_COMPLETED: 'CULTIVATION_COMPLETED',
  /** 物品状态更新 */
  INVENTORY_STATE_UPDATE: 'INVENTORY_STATE_UPDATE',
  /** 添加物品 */
  INVENTORY_ADD: 'INVENTORY_ADD',
  /** 添加装备物品 */
  INVENTORY_ADD_EQUIP: 'INVENTORY_ADD_EQUIP',
  /** 移除物品 */
  INVENTORY_REMOVE: 'INVENTORY_REMOVE',
  /** 使用物品 */
  INVENTORY_USE: 'INVENTORY_USE',
  /** 使用物品成功 */
  INVENTORY_USE_COMPLETED: 'INVENTORY_USE_COMPLETED',
  /** 背包物品-出售 */
  INVENTORY_SELL: 'INVENTORY_SELL',
  /** 背包物品-出售完成 */
  INVENTORY_SELL_COMPLETED: 'INVENTORY_SELL_COMPLETED',
  /** 临时背包-提取物品 */
  INVENTORY_TEMPORARY_EXTRACT: 'INVENTORY_TEMPORARY_EXTRACT',
  /** 增加灵根 */
  SPIRITUAL_ROOT_ADD: 'SPIRITUAL_ROOT_ADD',
  /** 减少灵根 */
  SPIRITUAL_ROOT_SUB: 'SPIRITUAL_ROOT_SUB',
  /** 提交灵根 */
  SPIRITUAL_ROOT_SUBMIT: 'SPIRITUAL_ROOT_SUBMIT',
  /** 灵根升级 */
  SPIRITUAL_ROOT_UPGRADE: 'SPIRITUAL_ROOT_UPGRADE',
  /** 灵根升级完成 */
  SPIRITUAL_ROOT_UPGRADE_COMPLETED: 'SPIRITUAL_ROOT_UPGRADE_COMPLETED',
  /** 灵根状态更新 */
  SPIRITUAL_ROOT_STATE_UPDATE: 'SPIRITUAL_ROOT_STATE_UPDATE',
  /** 设置主功法 */
  CULTIVATION_SKILL_SET_MAIN_CULTIVATION_SKILL:
    'CULTIVATION_SKILL_SET_MAIN_CULTIVATION_SKILL',
  /** 更新功法升级信息 */
  CULTIVATION_SKILL_UPDATE_UPGRADE_INFO:
    'CULTIVATION_SKILL_UPDATE_UPGRADE_INFO',
  /** 功法升级 */
  CULTIVATION_SKILL_UPGRADE: 'CULTIVATION_SKILL_UPGRADE',
  /** 功法升级成功 */
  CULTIVATION_SKILL_UPGRADE_SUCCESS: 'CULTIVATION_SKILL_UPGRADE_SUCCESS',
  /** 功法升级失败 */
  CULTIVATION_SKILL_UPGRADE_FAILED: 'CULTIVATION_SKILL_UPGRADE_FAILED',
  /** 功法状态更新 */
  CULTIVATION_SKILL_STATE_UPDATE: 'CULTIVATION_SKILL_STATE_UPDATE',
  /** 开始剧情 */
  STORY_START: 'STORY_START',
  /** 剧情结束 */
  STORY_END: 'STORY_END',
  /** UI 调用后剧情继续 */
  STORY_NOTIFY_UI_READY: 'STORY_NOTIFY_UI_READY',
  /** 剧情-选择选项 */
  STORY_MAKE_CHOICE: 'STORY_MAKE_CHOICE',
  /** 设置一个剧情完成 */
  STORY_SET_COMPLETED: 'STORY_SET_COMPLETED',
  /** 剧情状态更新 */
  STORY_STATE_UPDATE: 'STORY_STATE_UPDATE',
  /** 停止剧情 */
  STORY_STOP: 'STORY_STOP',
  /** 解锁功能成功 */
  FEATURE_UNLOCK_SUCCESS: 'FEATURE_UNLOCK_SUCCESS',
  /** 功能解锁状态更新 */
  FEATURE_UNLOCK_STATE_UPDATE: 'FEATURE_UNLOCK_STATE_UPDATE',
  /** 设置玩家是否已经初始化灵根 */
  PLAYER_PROGRESS_SET_INIT_SPIRITUAL_ROOT:
    'PLAYER_PROGRESS_SET_INIT_SPIRITUAL_ROOT',
  /** 玩家进度状态更新 */
  PLAYER_PROGRESS_STATE_UPDATE: 'PLAYER_PROGRESS_STATE_UPDATE',
  /** 设置离线时间 */
  OFFLINE_EARNINGS_SET_OFFLINE_TIME: 'OFFLINE_EARNINGS_SET_OFFLINE_TIME',
  /** 离线收益状态更新 */
  OFFLINE_EARNINGS_STATE_UPDATE: 'OFFLINE_EARNINGS_STATE_UPDATE',
  /** 领取离线收益 */
  OFFLINE_EARNINGS_GET_EARNINGS: 'OFFLINE_EARNINGS_GET_EARNINGS',
  /** 领取离线收益成功 */
  OFFLINE_EARNINGS_GET_EARNINGS_SUCCESS:
    'OFFLINE_EARNINGS_GET_EARNINGS_SUCCESS',
  /** 战斗等级升级 */
  BATTLE_LEVEL_UP: 'BATTLE_LEVEL_UP',
  /** 重置战斗等级 */
  BATTLE_LEVEL_RESET: 'BATTLE_LEVEL_RESET',
  /** 战斗等级状态更新 */
  BATTLE_LEVEL_STATE_UPDATE: 'BATTLE_LEVEL_STATE_UPDATE',
  /** 战斗状态更新 */
  BATTLE_STATE_UPDATE: 'BATTLE_STATE_UPDATE',
  /** 战斗结束 */
  BATTLE_END: 'BATTLE_END',
  /** 停止战斗 */
  BATTLE_STOP: 'BATTLE_STOP',
  /** 设置自动出售装备品质 */
  BATTLE_SET_AUTO_SELL_RARITY: 'BATTLE_SET_AUTO_SELL_RARITY',
  /** 玩家属性状态更新 */
  PLAYER_ATTRIBUTES_STATE_UPDATE: 'PLAYER_ATTRIBUTES_STATE_UPDATE',
  /** 场景状态更新 */
  SCENE_STATE_UPDATE: 'SCENE_STATE_UPDATE',
  /** 场景切换 */
  SCENE_CHANGE: 'SCENE_CHANGE',
  /** 退出当前场景 */
  SCENE_CURRENT_EXIT: 'SCENE_CURRENT_EXIT',
  /** 场景探索开始 */
  SCENE_EXPLORE_START: 'SCENE_EXPLORE_START',
  /** 场景探索完成 */
  SCENE_EXPLORE_COMPLETED: 'SCENE_EXPLORE_COMPLETED',
  /** 场景自动探索开始 */
  SCENE_AUTO_EXPLORE_START: 'SCENE_AUTO_EXPLORE_START',
  /** 场景停止探索 */
  SCENE_STOP_EXPLORE: 'SCENE_STOP_EXPLORE',
  /** 场景手动触发遭遇(debug) */
  SCENE_TRIGGER_MANUAL_ENCOUNTER: 'SCENE_TRIGGER_MANUAL_ENCOUNTER',
  /** 奖励发放 */
  REWARD_ISSUE: 'REWARD_ISSUE',
  /** 奖励发放完成 */
  REWARD_ISSUE_COMPLETED: 'REWARD_ISSUE_COMPLETED',
  /** 钱包状态更新 */
  WALLET_STATE_UPDATE: 'WALLET_STATE_UPDATE',
  /** 钱包扣除灵石 */
  WALLET_DEDUCT_SPIRIT_STONE: 'WALLET_DEDUCT_SPIRIT_STONE',
  /** 宗门状态更新 */
  SECT_STATE_UPDATE: 'SECT_STATE_UPDATE',
  /** 加入宗门 */
  SECT_JOIN: 'SECT_JOIN',
  /** 加入宗门-成功 */
  SECT_JOIN_SUCCESS: 'SECT_JOIN_SUCCESS',
  /** 宗门-挂机任务开始 */
  SECT_HANG_UP_TASK_START: 'SECT_HANG_UP_TASK_START',
  /** 宗门-挂机任务完成 */
  SECT_HANG_UP_TASK_COMPLETED: 'SECT_HANG_UP_TASK_COMPLETED',
  /** 宗门-挂机任务重置进度 */
  SECT_HANG_UP_TASK_RESET_PROGRESS: 'SECT_HANG_UP_TASK_RESET_PROGRESS',
  /** 宗门修炼场-增加修炼次数 */
  SECT_CULTIVATION_SPOT_ADD_VALID_COUNT:
    'SECT_CULTIVATION_SPOT_ADD_VALID_COUNT',
  /** 宗门职位-提升 */
  SECT_JOB_UPGRADE: 'SECT_JOB_UPGRADE',
  /** 宗门-兑换藏书阁物品 */
  SECT_EXCHANGE_SCRIPTURE_ITEM: 'SECT_EXCHANGE_SCRIPTURE_ITEM',
  /** 用户信息更新 */
  USER_INFO_STATE_UPDATE: 'USER_INFO_STATE_UPDATE',
  /** 修改用户名 */
  USER_INFO_CHANGE_USERNAME: 'USER_INFO_CHANGE_USERNAME',
  /** 装备面板状态更新 */
  EQUIPMENT_PANEL_STATE_UPDATE: 'EQUIPMENT_PANEL_STATE_UPDATE',
  /** 装备面板-穿装备 */
  EQUIPMENT_PANEL_EQUIP: 'EQUIPMENT_PANEL_EQUIP',
  /** 装备面板-穿装备完成 */
  EQUIPMENT_PANEL_EQUIP_COMPLETED: 'EQUIPMENT_PANEL_EQUIP_COMPLETED',
  /** 装备面板-卸装备 */
  EQUIPMENT_PANEL_UNEQUIP: 'EQUIPMENT_PANEL_UNEQUIP',
  /** 装备面板-卸装备完成 */
  EQUIPMENT_PANEL_UNEQUIP_COMPLETED: 'EQUIPMENT_PANEL_UNEQUIP_COMPLETED',
} as const;

type Events = {
  [EVENTS.GAME_START]: () => void;
  [EVENTS.GAME_STOP]: () => void;
  [EVENTS.GAME_DESTROYED]: () => void;
  [EVENTS.GAME_MESSAGE]: (message: string) => void;
  [EVENTS.GAME_AUTO_SAVE]: (data: string) => void;
  [EVENTS.GAME_MANUAL_SAVE]: (data: string) => void;
  [EVENTS.GAME_SAVE_ERROR]: (error: any) => void;
  [EVENTS.GAME_LOADED]: () => void;
  [EVENTS.GAME_LOAD_ERROR]: (error: any) => void;
  [EVENTS.CULTIVATION_START]: () => void;
  [EVENTS.CULTIVATION_COMPLETED]: () => void;
  [EVENTS.CULTIVATION_STATE_UPDATE]: () => void;
  [EVENTS.REALM_STATE_UPDATE]: () => void;
  [EVENTS.REALM_BREAKTHROUGH]: () => void;
  [EVENTS.INVENTORY_STATE_UPDATE]: () => void;
  [EVENTS.INVENTORY_ADD]: (defId: ItemId, amount: number) => void;
  [EVENTS.INVENTORY_ADD_EQUIP]: (inventorySlotItem: InventorySlotItem) => void;
  [EVENTS.INVENTORY_REMOVE]: (slotId: string, amount: number) => void;
  [EVENTS.INVENTORY_USE]: (
    slotId: string,
    itemId: string,
    amount: number
  ) => void;
  [EVENTS.INVENTORY_USE_COMPLETED]: () => void;
  [EVENTS.INVENTORY_SELL]: (
    inventorySlotItem: InventorySlotItem,
    amount: number
  ) => void;
  [EVENTS.INVENTORY_SELL_COMPLETED]: () => void;
  [EVENTS.INVENTORY_TEMPORARY_EXTRACT]: (tempSolts: TempSlot) => void;
  [EVENTS.SPIRITUAL_ROOT_ADD]: (key: SpiritualRootKey) => void;
  [EVENTS.SPIRITUAL_ROOT_SUB]: (key: SpiritualRootKey) => void;
  [EVENTS.SPIRITUAL_ROOT_SUBMIT]: () => void;
  [EVENTS.SPIRITUAL_ROOT_UPGRADE]: (key: SpiritualRootKey) => void;
  [EVENTS.SPIRITUAL_ROOT_UPGRADE_COMPLETED]: () => void;
  [EVENTS.SPIRITUAL_ROOT_STATE_UPDATE]: () => void;
  [EVENTS.CULTIVATION_SKILL_SET_MAIN_CULTIVATION_SKILL]: (
    id: CultivationSkillItemId
  ) => void;
  [EVENTS.CULTIVATION_SKILL_UPDATE_UPGRADE_INFO]: () => void;
  [EVENTS.CULTIVATION_SKILL_UPGRADE]: (id: string) => void;
  [EVENTS.CULTIVATION_SKILL_UPGRADE_SUCCESS]: () => void;
  [EVENTS.CULTIVATION_SKILL_UPGRADE_FAILED]: () => void;
  [EVENTS.CULTIVATION_SKILL_STATE_UPDATE]: () => void;
  [EVENTS.STORY_START]: (storyConfig: StoryId) => void;
  [EVENTS.STORY_END]: () => void;
  [EVENTS.STORY_NOTIFY_UI_READY]: () => void;
  [EVENTS.STORY_MAKE_CHOICE]: (index: number) => void;
  [EVENTS.STORY_SET_COMPLETED]: (id: StoryId) => void;
  [EVENTS.STORY_STATE_UPDATE]: () => void;
  [EVENTS.STORY_STOP]: () => void;
  [EVENTS.FEATURE_UNLOCK_SUCCESS]: (id: FeatureId) => void;
  [EVENTS.FEATURE_UNLOCK_STATE_UPDATE]: () => void;
  [EVENTS.PLAYER_PROGRESS_SET_INIT_SPIRITUAL_ROOT]: (flag: boolean) => void;
  [EVENTS.PLAYER_PROGRESS_STATE_UPDATE]: () => void;
  [EVENTS.OFFLINE_EARNINGS_SET_OFFLINE_TIME]: (time: number) => void;
  [EVENTS.OFFLINE_EARNINGS_STATE_UPDATE]: () => void;
  [EVENTS.OFFLINE_EARNINGS_GET_EARNINGS]: () => void;
  [EVENTS.OFFLINE_EARNINGS_GET_EARNINGS_SUCCESS]: () => void;
  [EVENTS.BATTLE_LEVEL_UP]: (data: LevelUpEvent) => void;
  [EVENTS.BATTLE_LEVEL_RESET]: (data: {
    oldLevel: number;
    newLevel: number;
  }) => void;
  [EVENTS.BATTLE_LEVEL_STATE_UPDATE]: () => void;
  [EVENTS.BATTLE_STATE_UPDATE]: () => void;
  [EVENTS.BATTLE_END]: (isWin: boolean) => void;
  [EVENTS.BATTLE_STOP]: () => void;
  [EVENTS.BATTLE_SET_AUTO_SELL_RARITY]: (rarity: ItemRarity[]) => void;
  [EVENTS.PLAYER_ATTRIBUTES_STATE_UPDATE]: () => void;
  [EVENTS.SCENE_STATE_UPDATE]: () => void;
  [EVENTS.SCENE_CHANGE]: (sceneId: string) => void;
  [EVENTS.SCENE_EXPLORE_START]: () => void;
  [EVENTS.SCENE_EXPLORE_COMPLETED]: () => void;
  [EVENTS.SCENE_AUTO_EXPLORE_START]: () => void;
  [EVENTS.SCENE_STOP_EXPLORE]: () => void;
  [EVENTS.SCENE_TRIGGER_MANUAL_ENCOUNTER]: () => void;
  [EVENTS.REWARD_ISSUE]: (
    reward: Reward[],
    sourceType: RewardSourceType
  ) => void;
  [EVENTS.REWARD_ISSUE_COMPLETED]: (sourceType: RewardSourceType) => void;
  [EVENTS.WALLET_STATE_UPDATE]: () => void;
  [EVENTS.WALLET_DEDUCT_SPIRIT_STONE]: (amount: number) => void;
  [EVENTS.SECT_STATE_UPDATE]: () => void;
  [EVENTS.SECT_JOIN]: (sectId: string) => void;
  [EVENTS.SECT_JOIN_SUCCESS]: () => void;
  [EVENTS.SECT_HANG_UP_TASK_START]: (id: string) => void;
  [EVENTS.SECT_HANG_UP_TASK_COMPLETED]: () => void;
  [EVENTS.SECT_HANG_UP_TASK_RESET_PROGRESS]: () => void;
  [EVENTS.SECT_CULTIVATION_SPOT_ADD_VALID_COUNT]: () => void;
  [EVENTS.SECT_JOB_UPGRADE]: () => void;
  [EVENTS.SECT_EXCHANGE_SCRIPTURE_ITEM]: (item: SectScriptureListItem) => void;
  [EVENTS.USER_INFO_STATE_UPDATE]: () => void;
  [EVENTS.USER_INFO_CHANGE_USERNAME]: (username: string) => void;
  [EVENTS.EQUIPMENT_PANEL_STATE_UPDATE]: () => void;
  [EVENTS.EQUIPMENT_PANEL_EQUIP]: (
    inventorySlotItem: InventorySlotItem
  ) => void;
  [EVENTS.EQUIPMENT_PANEL_EQUIP_COMPLETED]: () => void;
  [EVENTS.EQUIPMENT_PANEL_UNEQUIP]: (
    inventorySlotItem: InventorySlotItem
  ) => void;
  [EVENTS.EQUIPMENT_PANEL_UNEQUIP_COMPLETED]: () => void;
};

type TypeEventEmitter<T extends Record<keyof T, (...args: any[]) => any>> = {
  on<K extends keyof T>(event: K, listener: T[K]): void;
  off<K extends keyof T>(event: K, listener: T[K]): void;
  emit<K extends keyof T>(event: K, ...args: Parameters<T[K]>): boolean;
};

export type EventConfig = { cmd: typeof EVENTS.STORY_START; storyId: StoryId };

export type IEventBus = typeof bus;

export function emitEventConfig(events: EventConfig[]) {
  events.forEach((event) => {
    switch (event.cmd) {
      case EVENTS.STORY_START:
        bus.emit(EVENTS.STORY_START, event.storyId);
        break;
      default:
        break;
    }
  });
}

export const bus = new EventEmitter() as TypeEventEmitter<Events>;
